package com.devgroupvn.dota.items;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class GetItems
{

	private ArrayList<Item> listitems = new ArrayList<Item>();

	public GetItems()
	{
//		listitems =
	}

	public List<Item> getItemsList()
	{
		String url = "http://www.dota2wiki.com/wiki/Items";
		try
		{
			Document doc = Jsoup.connect(url).timeout(30000).get();
			Elements list = doc.select("table");
			boolean isFirst = false;
			for (Element element : list)
			{
				String type = element.previousElementSibling().text();
				
				if (isFirst == false) {
					type = element.previousElementSibling().previousElementSibling().text();
				}
				
				Elements items = element.select("td");
				isFirst = true;
				for (Element td : items)
				{
					Item item = new Item();
					item.type = type;
					
					if (td.select("img").first() != null)
						item.image = "http://www.dota2wiki.com" + td.select("img").first().attr("src");
					else item.image = "";
					item.name = td.text();
					item.url = "http://www.dota2wiki.com" + td.select("a").last().attr("href");
					
					item.IsNewGroup = false;
					
					if (isFirst) {
						item.IsNewGroup = true;
						isFirst = false;
					}
					
					listitems.add(item);
					System.out.println("-->>" + item.name);
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
		return listitems;
	}

	public String getItemDetail(String url)
	{
		Item i = new Item();
		try
		{
			Document doc = Jsoup.connect(url).timeout(30000).get();
			Element item = doc.select(".mw-content-ltr").first();
			i.description = item.html();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return i.description;
	}
}
